package com.axino.accessPlatform.test;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import com.axino.accessPlatform.bean.PrivateData;
import com.axino.accessPlatform.bean.PublicData;
import com.axino.accessPlatform.bean.RequestMode;
import com.axino.accessPlatform.exception.OpensnsException;
import com.axino.accessPlatform.service.OpenApiV1;
import com.axino.accessPlatform.util.EInvoiceSign;
import com.axino.accessPlatform.util.SecurityUtil;
import com.axino.accessPlatform.util.ValidataUtil;

/**
 * 电子发票  开票查询
 * @author Administrator
 *
 */
public class Echeck {

public static void main(String[] args) {
		
		//定义请求头数据
		Map<String,String> headers = getHeaders();
		//定义请求体中的公共数据
		PublicData pdData = getPublicData();
		//定义请求体中的业务数据
		PrivateData<Object> pvData = new PrivateData<Object>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> map_outsign = new HashMap<String,Object>();
		long startTime = System.currentTimeMillis();
		List<String> list_s = new ArrayList<String>();
		Map<String,Object> map_s = new HashMap<String,Object>();
//        map_s.put("taxNum", SecurityUtil.taxNum_value);
        map_outsign.put("timestamp", ""+startTime);
        map_outsign.put("appid", SecurityUtil.appid_value);
        map_outsign.put("businessSign", EInvoiceSign.eInvoiceEncodeData(""+startTime));
        map_s.put("outSign",map_outsign);
        list_s.add("17070513534801000013");
        map_s.put("invoiceSerialNum", list_s);
       
		list.add(map_s);
		System.out.println(map_s);
		pvData.setServicedata(list);//list
		RequestMode requestMode = getRequestMode(pdData,pvData);
		OpenApiV1 sdk = new OpenApiV1();
		String result = "";
		try {
			//调用服务
			result = sdk.handle(SecurityUtil.url,headers, requestMode);
			if(!ValidataUtil.isEmpty(result)){
				System.out.println("服务端的响应："+result);
			}
		} catch (OpensnsException e) {
			  System.out.printf("Request failed 【"+e.getErrorCode()+":"+e.getMessage()+"】");
			  e.printStackTrace();
		} 
	  }
	
	//定义请求头数据
	private static Map<String,String> getHeaders (){
		Map<String,String> headers = new HashMap<String, String>();
		headers.put("appKey", ""); //用户申请的appkey  J2VXxB2h_sbox
		headers.put("accessToken", ""); //用户Oauth登录后得到的令牌access token
		headers.put("compress", "");//压缩方式：提供GZIP 置空“”不压缩
		headers.put("signMethod", "AES"); //加密方式：提供AES ，不可为空
		headers.put("dataType", "JSON"); //数据请求格式： JSON/XML
		headers.put("appRate", SecurityUtil.app_rate); //app并发请求数  ，平台默认10如需升级请联系开放平台
		return headers;
	}
	
	//定义请求体中的公共数据
	private static PublicData getPublicData(){
		PublicData pdData = new PublicData();
		pdData.setVersion("1.0"); //API版本
		pdData.setTimestamp(String.valueOf(System.currentTimeMillis()));
		pdData.setMethod("nuonuo.electronInvoice.CheckEInvoice");
		return pdData;
	}
	
	private static RequestMode getRequestMode(PublicData pdata,PrivateData<Object> pvData){
		RequestMode requestMode = new RequestMode();
		requestMode.setPrivate(pvData);
		requestMode.setPublic(pdata);
		return requestMode;
	}

}
